class SuppliersController < ApplicationController
  before_action :set_supplier, only: [:show, :edit, :update, :destroy]

  # GET /suppliers
  # GET /suppliers.json
  def index
    @suppliers = Supplier.all
  end

  # GET /suppliers/1
  # GET /suppliers/1.json
  def show
  end

  # GET /suppliers/new
  def new
    @supplier = Supplier.new
  end

  # GET /suppliers/1/edit
  def edit
  end

  # POST /suppliers
  # POST /suppliers.json
  def create
    @drug = Drug.find(params[:drug_id])
  	if params[:commit] != "Create Supplier"
  		@supplier = Supplier.where(supplier_params)
  		@drug.suppliers << @supplier
  		respond_to do |format|
  			format.html { redirect_to @drug, notice: 'Manufacturer was successfully created.' }
  		end
  	else	
		#@supplier = Supplier.new(supplier_params)
		@supplier = @drug.suppliers.create(supplier_params)

		respond_to do |format|
		  if @supplier.save
			format.html { redirect_to @drug, notice: 'Supplier was successfully created.' }
			format.json { render :show, status: :created, location: @supplier }
		  else
			format.html { render :new }
			format.json { render json: @supplier.errors, status: :unprocessable_entity }
		  end
		end
	end
  end

  # PATCH/PUT /suppliers/1
  # PATCH/PUT /suppliers/1.json
  def update
    respond_to do |format|
      if @supplier.update(supplier_params)
        format.html { redirect_to @drug, notice: 'Supplier was successfully updated.' }
        format.json { render :show, status: :ok, location: @supplier }
      else
        format.html { render :edit }
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /suppliers/1
  # DELETE /suppliers/1.json
  def destroy
  @drug = Drug.find(params[:drug_id])
    @supplier.destroy
    respond_to do |format|
      format.html { redirect_to @drug, notice: 'Supplier was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_supplier
      @supplier = Supplier.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def supplier_params
      params.require(:supplier).permit(:name, :code, :website, :address, :email, :phone, :fax, :created_at, :updated_at, :created_by, :updated_by)
    end
end
